<?php

namespace app\admin\controller\user;

use app\admin\controller\Zlog;
use app\common\controller\Backend;
use app\common\logic\OrgLogic;
use think\Db;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Score extends Backend
{
    
    /**
     * Score模型对象
     * @var \app\admin\model\user\Score
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\Score;
        $this->view->assign("userTypeList", $this->model->getUserTypeList());
        $this->view->assign("userAuthTypeList", $this->model->getUserAuthTypeList());
        $this->assignconfig("org_cate_id", ORG_CATE_ID);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if(ORG_CATE_ID > 0){
                $child = (new OrgLogic())->getCateChild(ORG_CATE_ID);
                $where2 = ['score.user_type'=>1,'score.org_cate_id'=>['in', $child]];
            }else{
                $where2 = ['score.user_type'=>1];
            }

            $where3 = true;
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            if(isset($filter['orgcateid']) && $filter['orgcateid'] > 0){
                $child2 = (new OrgLogic())->getCateChild($filter['orgcateid']);
                $where3 = ['score.org_cate_id'=>['in', $child2]];
            }
            $total = $this->model
                ->with('user')
                ->join('edu_org_grade_user t', 't.user_id = score.id', 'left')
                ->join('edu_org_grade g', 'g.id = t.org_grade_id', 'left')
                ->where($where)
                ->where($where2)
                ->where($where3)
                ->count();
            $list = $this->model
                ->with('user')
                ->join('edu_org_grade_user t', 't.user_id = score.id', 'left')
                ->join('edu_org_grade g', 'g.id = t.org_grade_id', 'left')
                ->where($where)
                ->where($where2)
                ->where($where3)
                ->order('score.score', 'desc')
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => &$v) {
                $v['sort'] = ($k+1+$offset);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 搜索下拉列表
     */
    public function searchlist()
    {
        $result = Db::name('org_grade')->where('org_cate_id',ORG_CATE_ID)->order('type','asc')->order('group','asc')->select();
        $arr = [
            1=>'幼儿小班',
            2=>'幼儿中班',
            3=>'幼儿大班',
            4=>'一年级',
            5=>'二年级',
            6=>'三年级',
            7=>'四年级',
            8=>'五年级',
            9=>'六年级',
            10=>'初一年级',
            11=>'初二年级',
            12=>'初三年级',
            13=>'高一年级',
            14=>'高二年级',
            15=>'高三年级'];
        $searchlist = [];
        if($result){
            foreach($result as $k => $v){
                if(!empty($v['number'])){
                    $searchlist[] = ['id' => $v['id'],'name' => $arr[$v['group']].$v['number']];
                }

            }
        }
        $data = ['searchlist' => $searchlist];
        $this->success('', null, $data);
    }

    public function searchgroup(){
        $arr = [
            1=>'幼儿小班',
            2=>'幼儿中班',
            3=>'幼儿大班',
            4=>'一年级',
            5=>'二年级',
            6=>'三年级',
            7=>'四年级',
            8=>'五年级',
            9=>'六年级',
            10=>'初一年级',
            11=>'初二年级',
            12=>'初三年级',
            13=>'高一年级',
            14=>'高二年级',
            15=>'高三年级'];
        $searchlist = [];
        foreach($arr as $k => $v){
            $searchlist[] = ['id' => $k,'name' => $v];
        }
        $data = ['searchlist' => $searchlist];
        $this->success('', null, $data);
    }

}
